package com.sopaths.crawler.zhilian;

import java.util.Map;

import javax.annotation.Resource;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sopaths.contract.domain.Company;
import com.sopaths.contract.domain.CompanyType;
import com.sopaths.contract.domain.EducationBackground;
import com.sopaths.contract.domain.JobStatus;
import com.sopaths.contract.domain.Range;
import com.sopaths.contract.domain.Recruitment;
import com.sopaths.crawler.RecruitmentCrawler;
import com.sopaths.crawler.tasks.RecruitmentCrawlerTask;
import com.sopaths.crawler.util.ElementsHelper;

@Named
public class ZhiLianRecruitmentCrawler extends RecruitmentCrawler {

	private final Logger logger = Logger.getLogger(ZhiLianRecruitmentCrawler.class);

	@Resource(name="companySizeDictionary4ZhiLian")
	private Map<String, Range> companySizeDictionary;

	@Resource(name="companyTypeDictionary4ZhiLian")
	private Map<String, CompanyType> companyTypeDictionary;

	@Resource(name="experienceDictionary4ZhiLian")
	private Map<String, Range> experienceDictionary;

	@Resource(name="jobStatusDictionary4ZhiLian")
	private Map<String, JobStatus> jobStatusDictionary;

	@Resource(name="educationBackgroundDictionary4ZhiLian")
	private Map<String, EducationBackground> educationBackgroundDictionary;

	@Resource(name="salaryDictionary4ZhiLian")
	private Map<String, Range> salaryDictionary;

	@Resource(name="administrationExperienceDictionary4ZhiLian")
	private Map<String, Boolean> administrationExperienceDictionary;

	@Override
	protected String assembleRecruitmentUrl(RecruitmentCrawlerTask task) {
		return task.getUrl();
	}

	@Override
	protected void assembleRecruitmentTaskResult(RecruitmentCrawlerTask task, Document document) {
		try {
			Elements elements = document.select(".terminalpage-left");
			for (Element element : elements) {
				Recruitment recruitment = new Recruitment();
				recruitment.setUrl(task.getUrl());
				Company company = new Company();
				Elements companyNameElements = element.select(".terminalpage-table:nth-child(1) h2");
				company.setName(ElementsHelper.textUniqueElement(companyNameElements));
				Elements companySizeElements = element.select(".terminalpage-table:nth-child(1) tbody tr:nth-child(3) td:nth-child(2)");
				company.setSize(companySizeDictionary.get(ElementsHelper.textUniqueElement(companySizeElements)));
				Elements companyTypeElements = element.select(".terminalpage-table:nth-child(1) tbody tr:nth-child(4) td:nth-child(2)");
				company.setType(companyTypeDictionary.get(ElementsHelper.textUniqueElement(companyTypeElements)));
				Elements companyIndustryElements = element.select(".terminalpage-table:nth-child(1) tbody tr:nth-child(5) td:nth-child(2)");
				company.setIndustry(ElementsHelper.textUniqueElement(companyIndustryElements));
/*
				Elements companyAddressElements = element.select("div:containsOwn(公司地址：)");
				String companyAddressStr = ElementsHelper.textUniqueElement(companyAddressElements);
				if (companyAddressStr != null) {
					int startIndex = companyAddressStr.indexOf("公司地址：");
					int endIndex = companyAddressStr.indexOf("<br");
					if (endIndex != -1) {
						company.setAddress(companyAddressStr.substring(startIndex + 5, endIndex));
					} else {
						company.setAddress(companyAddressStr.substring(startIndex + 5));
					}
				}
*/
				Elements companyProfileElements = element.select("div:nth-child(3)>div:nth-child(2)");
				company.setProfile(ElementsHelper.textUniqueElement(companyProfileElements));
				recruitment.setCompany(company);

				recruitment.setJobTitle(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(1) h1")));
				recruitment.setExperience(experienceDictionary.get(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(1) td:nth-child(2)"))));
				recruitment.setJobStatus(jobStatusDictionary.get(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(1) td:nth-child(4)"))));
				recruitment.setEducationBackground(educationBackgroundDictionary.get(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(2) td:nth-child(2)"))));
				recruitment.setAdministrationExperience(administrationExperienceDictionary.get(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(2) td:nth-child(4)"))));
				recruitment.setSalary(salaryDictionary.get(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(3) td:nth-child(2)"))));
				recruitment.setRecruitingNumber(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(3) td:nth-child(4)")));
				recruitment.setPrimaryLocation(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(4) td:nth-child(2) #positionCityCon")));
				recruitment.setJobType(ElementsHelper.textUniqueElement(element.select(".terminalpage-table:nth-child(2) tbody tr:nth-child(4) td:nth-child(4)")));
				recruitment.setJobDescription(ElementsHelper.textUniqueElement(element.select("div:nth-child(2)>div.terminalpage-content")));
				recruitment.setContactInformation(ElementsHelper.textUniqueElement(element.select("div:nth-child(3)>div h2")));
				task.setResult(recruitment);
			}
		} catch (Exception ex) {
			logger.error("An error occurred while assemble recruitment. URL: " + task.getUrl(), ex);
		}
	}

	public void setCompanySizeDictionary(Map<String, Range> companySizeDictionary) {
		this.companySizeDictionary = companySizeDictionary;
	}

	public void setCompanyTypeDictionary(
			Map<String, CompanyType> companyTypeDictionary) {
		this.companyTypeDictionary = companyTypeDictionary;
	}

	public void setExperienceDictionary(Map<String, Range> experienceDictionary) {
		this.experienceDictionary = experienceDictionary;
	}

	public void setJobStatusDictionary(Map<String, JobStatus> jobStatusDictionary) {
		this.jobStatusDictionary = jobStatusDictionary;
	}

	public void setEducationBackgroundDictionary(
			Map<String, EducationBackground> educationBackgroundDictionary) {
		this.educationBackgroundDictionary = educationBackgroundDictionary;
	}

	public void setSalaryDictionary(Map<String, Range> salaryDictionary) {
		this.salaryDictionary = salaryDictionary;
	}

	public void setAdministrationExperienceDictionary(
			Map<String, Boolean> administrationExperienceDictionary) {
		this.administrationExperienceDictionary = administrationExperienceDictionary;
	}
}
